package leetcode2;

import leetcode.ListNode;

import java.util.Arrays;

public class NextLargerNodes {
    public int[] nextLargerNodes(ListNode head) {
        ListNode root = head;
        // 统计链表长度
        int len = 0;
        while (root != null) {
            len++;
            root = root.next;
        }
        int[] ans = new int[len];
        ListNode current = head;
        int count = 0;
        while (current != null) {
            int currentVal = current.val;
            ListNode next = current.next;
            // 从下一个开始遍历
            while (next != null) {
                if (next.val > currentVal) {
                    ans[count] = next.val;
                    break;
                }
                next = next.next;
            }
            current = current.next;
            count++;
        }
        return ans;
    }

    public static void main(String[] args) {
        ListNode header = new ListNode(2,new ListNode(1,new ListNode(5)));
        NextLargerNodes nextLargerNodes = new NextLargerNodes();
        int[] ans = nextLargerNodes.nextLargerNodes(header);
        System.out.println(Arrays.toString(ans));
    }
}
